package com.fzy.modules.people.service;

import com.fzy.core.common.HanyuPinyinCommon;
import com.fzy.core.common.IDUtils;
import com.fzy.core.common.PageObject;
import com.fzy.modules.people.dao.CustomerCategoryDao;
import com.fzy.modules.people.dao.CustomerDao;
import com.fzy.modules.people.entity.Customer;
import com.fzy.modules.people.entity.CustomerCategory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户操作
 */
@Service
public class CustomerService {
    @Autowired
   private CustomerDao customerDao;
    @Autowired
    private CustomerCategoryDao customerCategoryDao;
    /**
     * 查询所有客户信息
     */
    public PageObject<Customer> findAllByPage(Customer customer,int page,int rows){
        PageHelper.startPage(page, rows);
        List<Customer> Customers = customerDao.findAll(customer);
        //查询之后获取分页对象
        PageInfo<Customer> pageInfo=new PageInfo<Customer>(Customers);
        //构建我们自定义的分页实体返回类
        PageObject<Customer> pageObject=new PageObject<Customer>();
        pageObject.setTotal(pageInfo.getTotal());
        pageObject.setRows(Customers);
        return pageObject;
    }
    /**
     * 获得所有的code
     */
    public List<Customer> getCode(){
        return customerDao.getCode();
    }
    /**
     * 添加成功返回主键用于app数据库添加数据
     * @param customer
     * @return
     */
    public Customer addCustomer(Customer customer){
        customer.setId(IDUtils.genItemId());
        if(StringUtils.isEmpty(customer.getCompanyId())){
            customer.setCompanyId("4D74E1E4-A129-11E4-9B6A-782BCBD7746B");
        }
        if(StringUtils.isEmpty(customer.getDataOrg())){
            customer.setDataOrg("01010001");
        }
        customer.setPy(HanyuPinyinCommon.HanyuToPinYinStrFirstLetter(customer.getName()));
        if(customerDao.addCustomer(customer)==1){
        	CustomerCategory customerCategory = customerCategoryDao.findCustomerCategoryByid(customer.getCategoryId());
        	customer.setCategoryName(customerCategory.getName());
        	return customer;
        	
        }else{
        	return null;
        }
    }
    /**
     * 删除客户（存在销售订单的客户不可删除）
     */
    public Integer delCustomer(String id){
        return customerDao.delCustomer(id);
    }
    /**
     * 修改客户
     */
    public Integer updateCustomer(Customer customer){
        return customerDao.updateCustomer(customer);
    }
    /**
     * 修改客户时查询编码是否唯一
     */
    public List<Customer> isOne(Customer customer){
        return customerDao.isOne(customer);
    }
	public List<Customer> findAll() {
		Customer customer = new Customer();
		List<Customer> Customers = customerDao.findAll(customer);
		
		return Customers;
	}
}
